/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘远望
 * Date: 2025-05-16
 * Time: 22:25
 */
import java.util.*;
import java.util.ArrayList;

//剑值offer 57 牛客
public class Solution {
    //方法1暴力枚举
    public ArrayList<Integer> FindNumbersWithSum(int [] array, int sum) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < array.length; i++) {
            for (int j = i + 1; j < array.length; j++) {
                if (array[i] + array[j] == sum) {
                    list.add(array[i]);
                    list.add(array[j]);
                    return list;
                }
            }
        }

        return list;
    }

    //解法2.双指针
    /*public ArrayList<Integer> FindNumbersWithSum(int [] array, int sum) {
        //定义双指针
        int left = 0;
        int right = array.length - 1;
        ArrayList<Integer> list = new ArrayList<>();

        //利用有序数组的单调性省去很多不必要的比较
        while (left < right) {
            if (array[left] + array[right] > sum) {
                right--;
            }else if (array[left] + array[right] < sum) {
                left++;
            }else {
                list.add(array[left]);
                list.add(array[right]);
                return list;
            }
        }
        return list;
    }*/
}
